﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;

namespace QuanLyHangXeKhach
{
    public partial class frmCapNhatDiemDungChan : UserControl
    {
        CapNhatDiemDung cndd = new CapNhatDiemDung();
        DieuKhien dk = new DieuKhien();

        public frmCapNhatDiemDungChan()
        {
            InitializeComponent();
        }

        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmcndiemdung;
                
            }
            else
            {
                btndong.Command = null;
                
            }
        }

        #endregion

        private void frmCapNhatDiemDungChan_Load(object sender, EventArgs e)
        {
            Loadfrm();
        }

        private void Loadfrm()
        {
            List<thongtindiemdung> diemdung = cndd.laydiemdung();
            advTree1.Nodes.Clear();
            foreach (var row in diemdung)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.ma, row.ten));
            }
        }

        private Node CreatenoidenRow(string ma, string ten)
        {
            Node node = new Node(ma);            
            node.Cells.Add(new Cell(ten));
            return node;
        }

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            try
            {
                Node node = e.Node;
                txtmadd.Text = node.Cells[0].Text;
                txttendd.Text = node.Cells[1].Text;
            }
            catch (Exception) { }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Text = "Lưu";
            btnxoa.Text = "Hủy bỏ";
            btnthem.Enabled = false;
            txtmadd.Text = dk.NextID(cndd.madiemdunglonnhat().ToString(), "DDC");
            txttendd.Text = "";
            this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txttendd.Text == "")
                MessageBoxEx.Show("Nhập đủ thông tin trước khi tiếp tục", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (btnsua.Text == "Lưu")
                {
                    cndd.themdiemdung(txtmadd.Text.Trim(), txttendd.Text);
                    MessageBoxEx.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtmadd.Text = dk.NextID(cndd.madiemdunglonnhat().ToString(), "DDC");
                    txttendd.Text = "";
                    Loadfrm();
                }
                else
                {
                    cndd.suadiemdung(txtmadd.Text.Trim(), txttendd.Text);
                    MessageBoxEx.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Loadfrm();
                }
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnxoa.Text == "Hủy bỏ")
            {
                btnthem.Enabled = true;
                btnsua.Text = "Sửa";
                btnxoa.Text = "Xóa";
                txtmadd.Text = "";
                txttendd.Text = "";
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                if (txtmadd.Text == "")
                    MessageBoxEx.Show("chọn đối tượng muốn xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    DialogResult dr = MessageBoxEx.Show("Bạn muốn xóa điểm dừng " + txttendd.Text + " này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (dr == DialogResult.Yes)
                    {
                        cndd.xoadiemdung(txtmadd.Text.Trim());
                        MessageBoxEx.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Loadfrm();
                    }
                }
            }
        }


    }
}
